package com.tj.collection.db.domain;

import com.fqgj.common.utils.CollectionUtils;
import com.tj.collection.db.entity.ChatterConfigEntity;
import com.tj.collection.db.entity.RptChatterDataPreDayEntity;
import com.tj.collection.db.entity.RptChatterOnlinePreHourEntity;
import com.tj.collection.enums.ChatterConfigTypeEnum;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* TableDesc:chatter 每日数据统计
*/
public class RptChatterDataPreDayDto {
    /**
    *
    */
    private String chatterUserCode;
    /**
    *统计日期
    */
    private Integer createDt;
    /**
     * 在线用户数
     */
    private OnlineUser onlineUser;
    /**
    *总在线用户数
    */
    private Integer onlineUserTotal;
    /**
    *活跃会话数
    */
    private Integer activeChatsCnt;
    /**
    *新建联数
    */
    private NewRelationCnt newRelationCnt;
    /**
    *用户给chatter发送的消息总数
    */
    private Integer userSendMsgCnt;
    /**
    *chatter回复消息总数
    */
    private Integer replyCnt;
    /**
    *破冰会话数
    */
    private BreakIceChat breakIceChatCnt;

    /**
    *有效会话数
    */
    private Integer validChatsCnt;
    /**
    *有效会话数
    */
    private Integer deepChatsCnt;
    /**
    *合计卖出PP个数
    */
    private Integer totalPpCnt;
    /**
    *合计卖出PV个数
    */
    private Integer totalPvCnt;
    /**
    *合计卖出PPV个数
    */
    private TotalPPv totalPpvCnt;

    public String getChatterUserCode() {
        return chatterUserCode;
    }

    public void setChatterUserCode(String chatterUserCode) {
        this.chatterUserCode = chatterUserCode;
    }

    public Integer getCreateDt() {
        return createDt;
    }

    public void setCreateDt(Integer createDt) {
        this.createDt = createDt;
    }

    public OnlineUser getOnlineUser() {
        return onlineUser;
    }

    public void setOnlineUser(OnlineUser onlineUser) {
        this.onlineUser = onlineUser;
    }

    public Integer getOnlineUserTotal() {
        return onlineUserTotal;
    }

    public void setOnlineUserTotal(Integer onlineUserTotal) {
        this.onlineUserTotal = onlineUserTotal;
    }

    public Integer getActiveChatsCnt() {
        return activeChatsCnt;
    }

    public void setActiveChatsCnt(Integer activeChatsCnt) {
        this.activeChatsCnt = activeChatsCnt;
    }

    public NewRelationCnt getNewRelationCnt() {
        return newRelationCnt;
    }

    public void setNewRelationCnt(NewRelationCnt newRelationCnt) {
        this.newRelationCnt = newRelationCnt;
    }

    public Integer getUserSendMsgCnt() {
        return userSendMsgCnt;
    }

    public void setUserSendMsgCnt(Integer userSendMsgCnt) {
        this.userSendMsgCnt = userSendMsgCnt;
    }

    public Integer getReplyCnt() {
        return replyCnt;
    }

    public void setReplyCnt(Integer replyCnt) {
        this.replyCnt = replyCnt;
    }

    public BreakIceChat getBreakIceChatCnt() {
        return breakIceChatCnt;
    }

    public void setBreakIceChatCnt(BreakIceChat breakIceChatCnt) {
        this.breakIceChatCnt = breakIceChatCnt;
    }

    public Integer getValidChatsCnt() {
        return validChatsCnt;
    }

    public void setValidChatsCnt(Integer validChatsCnt) {
        this.validChatsCnt = validChatsCnt;
    }

    public Integer getDeepChatsCnt() {
        return deepChatsCnt;
    }

    public void setDeepChatsCnt(Integer deepChatsCnt) {
        this.deepChatsCnt = deepChatsCnt;
    }

    public Integer getTotalPpCnt() {
        return totalPpCnt;
    }

    public void setTotalPpCnt(Integer totalPpCnt) {
        this.totalPpCnt = totalPpCnt;
    }

    public Integer getTotalPvCnt() {
        return totalPvCnt;
    }

    public void setTotalPvCnt(Integer totalPvCnt) {
        this.totalPvCnt = totalPvCnt;
    }

    public TotalPPv getTotalPpvCnt() {
        return totalPpvCnt;
    }

    public void setTotalPpvCnt(TotalPPv totalPpvCnt) {
        this.totalPpvCnt = totalPpvCnt;
    }

    public static RptChatterDataPreDayDto toDto(RptChatterDataPreDayEntity entity, List<ChatterConfigEntity> chatterConfigs, List<RptChatterOnlinePreHourEntity> chatterOnlinePreHourEntities, Map<String,RptChatterOnlinePreHourEntity> onlinePreHourTotalMap) {
        final RptChatterDataPreDayDto dto = new RptChatterDataPreDayDto();
        if (entity == null) {
            return dto;
        }

        BeanUtils.copyProperties(entity, dto);


        // chatter时段在线用户
        dto.setOnlineUser(OnlineUser.builder().onlineUserCnt(0).showWarningFlag(0).build());
        if (CollectionUtils.isNotEmpty(chatterOnlinePreHourEntities)) {
            final RptChatterOnlinePreHourEntity online = chatterOnlinePreHourEntities.parallelStream().filter(o -> o.getChatterUserCode().equals(entity.getChatterUserCode())).findFirst().orElse(null);
            //对今日有在线用户的进行添加
            Integer onlineUserNumToday = 0;
            if(Objects.nonNull(onlinePreHourTotalMap) && onlinePreHourTotalMap.containsKey(entity.getChatterUserCode())){
                onlineUserNumToday = onlinePreHourTotalMap.get(entity.getChatterUserCode()).getOnlineUserCnt();
            }

            dto.setOnlineUser(OnlineUser.builder().onlineUserTotal(onlineUserNumToday).onlineUserCnt(0).showWarningFlag(0).build());
            if (online != null) {
                dto.setOnlineUserTotal(onlineUserNumToday);
                dto.setOnlineUser(OnlineUser.builder().onlineUserTotal(onlineUserNumToday).onlineUserCnt(online.getOnlineUserCnt()).showWarningFlag(online.getWarningFlag()).build());
            }
        }

        // 新建联
        final int newRelationCnt = entity.getNewRelationCnt() == null ? 0 : entity.getNewRelationCnt();
        final Integer relationTargetValue = getTargetValue(chatterConfigs, entity.getChatterUserCode(), ChatterConfigTypeEnum.NEW_RELATION_PRE_DAY.getType());
        dto.setNewRelationCnt(NewRelationCnt.builder().newRelationCnt(entity.getNewRelationCnt()).showWarningFlag(newRelationCnt < relationTargetValue ? 1 : 0).build());

        // 破冰会话
        final int breakIceChatCnt = entity.getBreakIceChatCnt() == null ? 0 : entity.getBreakIceChatCnt();
        final Integer iceBreakTargetValue = getTargetValue(chatterConfigs, entity.getChatterUserCode(), ChatterConfigTypeEnum.BREAK_ICE_CHAT_PRE_DAY.getType());
        dto.setBreakIceChatCnt(BreakIceChat.builder().breakIceChatCnt(entity.getBreakIceChatCnt()).showWarningFlag(breakIceChatCnt < iceBreakTargetValue ? 1 : 0).build());

        // PPV
        final int ppvCnt = entity.getTotalPpvCnt() == null ? 0 : entity.getTotalPpvCnt();
        final Integer ppvTargetValue = getTargetValue(chatterConfigs, entity.getChatterUserCode(), ChatterConfigTypeEnum.TOTAL_PPV_PRE_DAY.getType());
        dto.setTotalPpvCnt(TotalPPv.builder().totalPPvCnt(entity.getTotalPpvCnt()).showWarningFlag(ppvCnt < ppvTargetValue ? 1 : 0).build());

        return dto;
    }

    private static Integer getTargetValue(List<ChatterConfigEntity> allChatterConfigs, String chatterUserCode, Integer type) {
        if (CollectionUtils.isNotEmpty(allChatterConfigs)) {
            return allChatterConfigs.parallelStream().filter(o -> o.getChatterCode().equals(chatterUserCode)).filter(o -> o.getType().equals(type)).map(ChatterConfigEntity::getGoalValue).findFirst().orElse(0);
        }
        return 0;
    }
}
